<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:set value="${pageContext.servletContext.contextPath }" var="ctx"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>左边</title>

    <link rel="stylesheet" href="${ctx}/resources/css/jquery.treeview.css" />
    <script src="${ctx}/resources/js/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script src="${ctx}/resources/js/jquery.treeview.js" type="text/javascript"></script>
    <style type="text/css">
        body {
            font-family: "Microsoft YaHei" ! important;
            margin-left: 0px;
            margin-top: 0px;
            margin-right: 0px;
            margin-bottom: 0px;
            background: url(${ctx}/resources/images/leftbg_02.gif) repeat-y;
        }
        li,ul,li a{ list-style:none; text-decoration:none; margin:0px; padding:0px;}
    </style>
    <script type="text/javascript">
        $(document).ready(	function() {
            var createLi=function(menu,parent){
                var li=$("<li>");
                var hasChild=menu.childs.length>0;
                if(hasChild){
                    li.append($("<span>").text(menu.name));
                    var ul = $("<ul>");
                    li.append(ul);
                    parent.append(li);
                    for(i in menu.childs){
                        createLi(menu.childs[i], ul);
                    }
                }else{
                    li.append($("<span>").text(menu.name).attr("href","${ctx}" + menu.resUrl));
                    parent.append(li);
                }
            };
            var sort=function(items){
                for (var i = 0; i < items.length; i++) {
                    for(var j = i+1; j < items.length; j++) {
                        if (items[i].level > items[j].level) {
                            t = items[i];
                            items[i] = items[j];
                            items[j] = t;
                        }
                    }
                    if(items[i].childs.length>0){
                        items[i].childs=sort(items[i].childs);
                    }
                }
                return items;
            };
            var buildTreeDOM=function(){
                //链接加上参数 避免同一游览器 切换用户登录时候 菜单栏 被缓存
                var no_cache = '?v=' + new Date().getTime();
                $.getJSON("${ctx}/getMenus.do" + no_cache,{},function(data){
                    data=sort(data);
                    $.each(data,function(i,n){
                        createLi(n,$("#navigation"));
                    });
                    $("#navigation").treeview({
                        animated : "fast",
                        collapsed : true,
                        unique : true,
                        toggle : function() {
                        }
                    });
                    $("#navigation li span").each(function(i, n) {
                        if ($(n).attr("href") != undefined) {
                            $(n).click(function() {
                                var href = $(this).attr("href");
                                if(href ==''||href == undefined||href=="${ctx}"){
                                    alert("此功能还在开发中..");
                                    return;
                                }
                                window.parent.frames["main"].location.href =
                                        $(this).attr("href");
                            });
                        }
                    });
                });
            };

            buildTreeDOM();

            $('#navigation li:not(:has(ul))  span')
                    .mouseover(function() {
                        this.style.background = "url(${ctx}/resources/pics/m_l_l_leftsidebar.gif)";
                    }).mouseout(function() {
                        this.style.background = "none";
                    });
        });
    </script>

</head>

<body>
<div class="leftMain" style="width:171px; overflow:hidden;">

    <div class="left">
        <ul id="navigation">
        </ul>
    </div>
</div>
</body>
</html>